package com.vipboot.framework.mybatisplus.core.handler;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.vipboot.framework.core.domain.LoginUser;
import com.vipboot.framework.core.exception.ServiceException;
import com.vipboot.framework.satoken.helper.LoginHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * MP注入处理器
 */
@Slf4j
public class InjectionMetaObjectHandler implements MetaObjectHandler {

    private final static String CreateTime = "createTime";
    private final static String CreateBy = "createBy";
    private final static String CreateDept = "createDept";
    private final static String UpdateTime = "updateTime";
    private final static String UpdateBy = "updateBy";
//    private final static String DeleteTime = "deleteTime";
//    private final static String DeleteBy = "deleteBy";
//    private final static String Version = "version";

    /**
     * 插入填充方法，用于在插入数据时自动填充实体对象中的创建时间、更新时间、创建人、更新人等信息
     *
     * @param metaObject 元对象，用于获取原始对象并进行填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("全局插入数据自动注入开始 {}", metaObject);
        try {
            if (ObjectUtil.isNotNull(metaObject)) {

                Date date = new Date();
                this.strictInsertFill(metaObject, CreateTime, Date.class, date);
                this.strictInsertFill(metaObject, UpdateTime, Date.class, date);

                LoginUser loginUser = getLoginUser();
                if (ObjectUtil.isNotNull(loginUser)) {
                    this.strictInsertFill(metaObject, CreateBy, Long.class, loginUser.getUserId());
                    this.strictInsertFill(metaObject, CreateDept, Long.class, loginUser.getDeptId());
                }
            }
        } catch (Exception e) {
            throw new ServiceException("全局插入数据监听器注入异常 => " + e.getMessage());
        }
    }

    /**
     * 更新填充方法，用于在更新数据时自动填充实体对象中的更新时间和更新人信息
     *
     * @param metaObject 元对象，用于获取原始对象并进行填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("全局更新数据自动注入开始 {}", metaObject);
        try {
            if (ObjectUtil.isNotNull(metaObject)) {
                Date date = new Date();
                this.strictUpdateFill(metaObject, UpdateTime, Date.class, date);
                LoginUser loginUser = getLoginUser();
                if (ObjectUtil.isNotNull(loginUser)) {
                    this.strictUpdateFill(metaObject, UpdateBy, Long.class, loginUser.getUserId());
                }
            }
        } catch (Exception e) {
            throw new ServiceException("全局更新数据监听器注入异常 => " + e.getMessage());
        }
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 当前登录用户的信息，如果用户未登录则返回 null
     */
    private LoginUser getLoginUser() {
        LoginUser loginUser;
        try {
            loginUser = LoginHelper.getLoginUser();
        } catch (Exception e) {
            log.warn("全局自动注入警告 => 用户未登录");
            return null;
        }
        return loginUser;
    }
}
